/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.verolux.soft.debaraz.dao;

import com.verolux.soft.debaraz.ApplicationException;
import com.verolux.soft.debaraz.bean.User;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;

/**
 *
 * @author ut1e8p
 */
@Repository(value="userDAO")
public class UserDAOImpl implements UserDAO {

    @PersistenceContext
    private EntityManager entityManager;

    /**
     *
     * @param user
     * @throws ApplicationException
     */
    public void save(User user) throws ApplicationException {
        try {
            entityManager.persist(user);
        } catch (Exception e){
            throw new ApplicationException(e.getMessage(), e);
        }
    }

    /**
     *
     * @param user
     * @throws ApplicationException
     */
    public void delete(User user) throws ApplicationException {
        try {
            entityManager.remove(user);
        } catch (Exception e){
            throw new ApplicationException(e.getMessage(), e);
        }
    }

    /**
     *
     * @param email
     * @return
     * @throws ApplicationException
     */
    public User findByEmail(String email) throws ApplicationException {
         try {
            return entityManager.find(User.class, email);
        } catch (Exception e){
            throw new ApplicationException(e.getMessage(), e);
        }
    }
}
